package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.MessageLogic;
import cn.topevery.garden.entity.dto.MessageDTO;
import cn.topevery.garden.entity.po.Message;
import cn.topevery.garden.entity.query.MessageQuery;
import cn.topevery.garden.entity.vo.MessageVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 留言表
 *
 * @author code-generate
 * @date 2022-02-18
 */
@Api(tags = "留言表")
@RestController
@RequestMapping("/message")
public class MessageController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    MessageLogic messageLogic;


    /**
     * 获取留言表分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取留言表页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<MessageVO>> listPage(@RequestBody MessageQuery query) {
        return JsonResult.ok(messageLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 留言表信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody MessageDTO dto) {
        messageLogic.saveOrUpdate(getUser().id.toString(),mapper.map(dto, Message.class));
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<MessageVO> getInfoById(@RequestParam Integer id) {
        Message entity = messageLogic.getEntity(id);
        MessageVO vo= mapper.map(entity, MessageVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        messageLogic.deleteBatchById(getUser().id.toString(),ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出留言表信息")
    @GetMapping("/export")
    public void export(MessageQuery query, HttpServletResponse response) {
        List<MessageVO> list = messageLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "留言表", "sheet1", MessageVO.class, "留言表", response);
    }

}
